import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
// 引入按需引入element-plus组件的插件
import AutoImport from 'unplugin-auto-import/vite'
import Component from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// 导入自适应css插件
import postCssPxToRem from 'postcss-pxtorem'

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src'),
        // 增加别名
        '@store': resolve('src/renderer/src/store'),
        '@views': resolve('src/renderer/src/views'),
        '@components': resolve('src/renderer/src/components'),
        '@utils': resolve('src/renderer/src/utils'),
        '@assets': resolve('src/renderer/src/assets'),
        '@network': resolve('src/renderer/src/network'),
        '@router': resolve('src/renderer/src/router'),
        '@hooks': resolve('src/renderer/src/hooks'),
        '@types': resolve('src/renderer/src/types'),
        '@api': resolve('src/renderer/src/api')
      }
    },
    plugins: [
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Component({
        resolvers: [ElementPlusResolver()]
      }),
      vue()
    ],
    css:{
      postcss:{
        plugins: [
          postCssPxToRem({
            // 自适应css的配置
            rootValue: 192, // pc端建议192，移动端建议75；设计稿宽度的1/10
            propList: ['*','!border'], // 除 border 外所有px 转 rem // 需要转换的属性，这里选择全部都进行转换
            selectorBlackList: ['norem'], // 过滤掉norem开头的class，不进行rem转换
            unitPrecision: 5, // 转换后的精度，即小数点位数
            replace: true, // 是否直接替换属性值，而不是添加新属性
            mediaQuery: false, // 是否添加媒体查询，即@media screen and (min-width: 750px)
            minPixelValue: 0 // 最小的转换像素值，如果为1的话，只有大于1的像素值才会被转换
          })
        ]
      }
    },
    // 配置代理
    server: {
      proxy: {
        '/api': {
          target: 'http://localhost:3000',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }
  }
})
